package mi.payment.ui.print.helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import mi.payment.R;
import mi.payment.constants.CARDTYPE;
import mi.payment.constants.TransType;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.utils.NumberUtil;
import mi.payment.utils.SystemDateTime;

import static mi.payment.utils.UIUtils.getString;

/**
 * PackageName: mi.payment.ui.print.helper
 * FileName: CardTicketView
 * Created by longx on 2018-12-20 15:29
 */
public class CardTicketView extends LinearLayout {

    private ImageView logo, signatureImg;
    private TextView addressTextView, tidTextView, midTextView, batchTextView, traceTextView,
            dateTextView, cardTypeTextView, cardNumberTextView, icData1TextView, icData2TextView,
            totalTextView, signatureTextTextView, signatureHintTextTextView, sloganeerTextView,
            leagueTextView, versionTextView, transactionTypeTextView;
    private LinearLayout linIcData;

    public CardTicketView(Context context) {
        super(context);
        initView();
    }

    public CardTicketView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CardTicketView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View.inflate(getContext(), R.layout.view_card_ticket, this);
        logo = this.findViewById(R.id.logo);
        signatureImg = this.findViewById(R.id.img_signature);
        linIcData = this.findViewById(R.id.lin_ic_data);
        addressTextView = this.findViewById(R.id.address);
        tidTextView = this.findViewById(R.id.tid);
        midTextView = this.findViewById(R.id.mid);
        batchTextView = this.findViewById(R.id.batch);
        traceTextView = this.findViewById(R.id.trace);
        dateTextView = this.findViewById(R.id.date);
        cardTypeTextView = this.findViewById(R.id.card_type);
        cardNumberTextView = this.findViewById(R.id.card_number);
        icData1TextView = this.findViewById(R.id.ic_data1);
        icData2TextView = this.findViewById(R.id.ic_data2);
        totalTextView = this.findViewById(R.id.total);
        signatureTextTextView = this.findViewById(R.id.text_signature);
        signatureHintTextTextView = this.findViewById(R.id.text_signature_hint);
        sloganeerTextView = this.findViewById(R.id.sloganeer);
        leagueTextView = this.findViewById(R.id.league);
        versionTextView = this.findViewById(R.id.version);
        transactionTypeTextView = this.findViewById(R.id.transaction_type);
    }

    @SuppressLint("SetTextI18n")
    public void setPayDetail(PayDetail payDetail) {

        logo.setImageResource(R.drawable.print_thrid);

        // TODO: 2018-12-20 设置地址

        // 打印商户号
        String merchantNo = TextUtils.isEmpty(payDetail.tempMerchantNo) ? payDetail.merchantNo : payDetail.tempMerchantNo;
        merchantNo = TextUtils.isEmpty(merchantNo) ? "--" : merchantNo;
        String temp = "MID: " + merchantNo;
        midTextView.setText(temp);

        // 打印终端号
        String terminalNo = TextUtils.isEmpty(payDetail.tempTerminalNo) ? payDetail.terminalNo : payDetail.tempTerminalNo;
        terminalNo = TextUtils.isEmpty(terminalNo) ? "--" : terminalNo;
        temp = "TID: " + terminalNo;
        tidTextView.setText(temp);

        //批次号
        String batch = TextUtils.isEmpty(payDetail.getBathNo()) ? " " : payDetail.getBathNo();
        temp = "Batch: " + batch;
        batchTextView.setText(temp);

        //流水号
        String trace = TextUtils.isEmpty(payDetail.getVoucherNo()) ? " " : payDetail.getVoucherNo();
        temp = "Trace: " + trace;
        traceTextView.setText(temp);

        //日期
        try {
            String month = getEnglishMonth(payDetail.TradeDate.substring(0, 2));
            String day = payDetail.TradeDate.substring(0, 2);
            String transTime;
            if (TextUtils.isEmpty(payDetail.TradeTime)) {
                transTime = "";
            } else {
                transTime = payDetail.TradeTime.substring(0, 2) + ":" + payDetail.TradeTime.substring(2, 4) + ":" + payDetail.TradeTime.substring(4, 6);
            }
            temp = month + " " + day + "," + SystemDateTime.getYYYY().substring(2, 4) + " " + transTime;
            dateTextView.setText(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // TODO: 2018-12-20 CardType

        transactionTypeTextView.setText(TransType.getName(payDetail.transType) + "e");

        //卡号
        String cardStr;
        if (payDetail.transactionType == 4) {
            if (SystemParamsOperation.getInstance().getSystemParamsSettings().isShieldAuthorizationCard()) {
                cardStr = NumberUtil.intercept(payDetail.CardNo);
            } else {
                cardStr = payDetail.CardNo;
            }
        } else {
            cardStr = NumberUtil.intercept(payDetail.CardNo);
        }

        String st;
        if (payDetail.cardType == CARDTYPE.MAGNETIC.getValue()) {
            st = "  S";
        } else if (payDetail.cardType == CARDTYPE.IC.getValue()) {
            st = "  I";
        } else if (payDetail.cardType == CARDTYPE.CONTACTLESS.getValue()) {
            st = "  C";
        } else {
            st = "  M";
        }
        cardNumberTextView.setText(cardStr + st);

        if ((payDetail.cardType == CARDTYPE.IC.getValue() || payDetail.cardType == CARDTYPE.CONTACTLESS.getValue()) && payDetail.getTransactionType() != 3) {
            linIcData.setVisibility(VISIBLE);
            temp = "ARQC:" + payDetail.getArqC()
                    + "\nAID:" + payDetail.getAID()
                    + "\nTVR:" + payDetail.getTVR()
                    + "\nATC:" + payDetail.getATC();
            icData1TextView.setText(temp);
            temp = "APP_LABLE:" + payDetail.getAppLabel()
                    + "\nAPP_NAME:" + payDetail.getAppName()
                    + "\nTC:" + payDetail.getTC()
                    + "\nTSI:" + payDetail.getTSI();
            icData2TextView.setText(temp);
        } else {
            linIcData.setVisibility(GONE);
        }

        // TODO: 2018-12-20 TOTAL

        //是否免密
        boolean isFreePwd = payDetail.isFreePWD;
        //是否免签
        boolean isFreeSign = payDetail.isFreeSign;

        signatureHintTextTextView.setVisibility(isFreePwd ? GONE : VISIBLE);
        signatureTextTextView.setVisibility(isFreeSign ? GONE : VISIBLE);

        // TODO: 2018-12-20  version 第几联

    }


    /**
     * 一月Jan，二月Feb，三月Mar，四月Apr，五月May，六月June，七月July，八月Aug，九月Sept，十月Oct，十一月Nov，十二月Dec。
     *
     * @param strNumMonth 01,1,02,2,3,03,4,04,5,05,6,06,7,07,8,08,9,09,10,11,12
     * @return 一月Jan，二月Feb，三月Mar，四月Apr，五月May，六月June，七月July，八月Aug，九月Sept，十月Oct，十一月Nov，十二月Dec。
     */
    private static String getEnglishMonth(String strNumMonth) {
        try {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("MM");
            Date date = sdf.parse(strNumMonth);
            sdf = new SimpleDateFormat("MMMMM", Locale.US);
            return sdf.format(date).substring(0, 3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return " ";
    }

}
